#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define MAX_TASKS 100
#define MAX_TASK_LENGTH 256
#define DATA_FILE "tasks.dat"

// 任务结构体
typedef struct {
    int id;
    char description[MAX_TASK_LENGTH];
    int completed;
    time_t created_at;
    time_t completed_at;
} Task;

// 全局任务数组和计数器
Task tasks[MAX_TASKS];
int task_count = 0;

// 函数声明
void load_tasks();
void save_tasks();
void add_task(const char *description);
void list_tasks();
void mark_task_completed(int id);
void delete_task(int id);
void print_task(Task task);
void print_usage();

int main(int argc, char *argv[]) {
    // 加载已保存的任务
    load_tasks();
    
    printf("=== 待办事项管理器 ===\n");
    
    if (argc < 2) {
        print_usage();
        return 1;
    }
    
    if (strcmp(argv[1], "add") == 0) {
        if (argc < 3) {
            printf("错误: 请提供任务描述\n");
            return 1;
        }
        add_task(argv[2]);
    } else if (strcmp(argv[1], "list") == 0) {
        list_tasks();
    } else if (strcmp(argv[1], "complete") == 0) {
        if (argc < 3) {
            printf("错误: 请提供任务ID\n");
            return 1;
        }
        mark_task_completed(atoi(argv[2]));
    } else if (strcmp(argv[1], "delete") == 0) {
        if (argc < 3) {
            printf("错误: 请提供任务ID\n");
            return 1;
        }
        delete_task(atoi(argv[2]));
    } else {
        print_usage();
        return 1;
    }
    
    // 保存任务到文件
    save_tasks();
    
    return 0;
}

void load_tasks() {
    FILE *file = fopen(DATA_FILE, "rb");
    if (file == NULL) {
        // 文件不存在，初始化为空
        task_count = 0;
        return;
    }
    
    fread(&task_count, sizeof(int), 1, file);
    if (task_count > MAX_TASKS) {
        task_count = MAX_TASKS;
    }
    
    fread(tasks, sizeof(Task), task_count, file);
    fclose(file);
}

void save_tasks() {
    FILE *file = fopen(DATA_FILE, "wb");
    if (file == NULL) {
        printf("错误: 无法保存任务\n");
        return;
    }
    
    fwrite(&task_count, sizeof(int), 1, file);
    fwrite(tasks, sizeof(Task), task_count, file);
    fclose(file);
}

void add_task(const char *description) {
    if (task_count >= MAX_TASKS) {
        printf("错误: 任务数量已达上限\n");
        return;
    }
    
    // 生成唯一ID
    int new_id = 1;
    for (int i = 0; i < task_count; i++) {
        if (tasks[i].id >= new_id) {
            new_id = tasks[i].id + 1;
        }
    }
    
    Task new_task;
    new_task.id = new_id;
    strncpy(new_task.description, description, MAX_TASK_LENGTH - 1);
    new_task.description[MAX_TASK_LENGTH - 1] = '\0';
    new_task.completed = 0;
    new_task.created_at = time(NULL);
    new_task.completed_at = 0;
    
    tasks[task_count++] = new_task;
    printf("任务已添加 (ID: %d)\n", new_id);
}

void list_tasks() {
    if (task_count == 0) {
        printf("当前没有待办任务\n");
        return;
    }
    
    printf("\n待办任务列表:\n");
    printf("----------------------------------------\n");
    
    // 先打印未完成的任务
    int completed_count = 0;
    for (int i = 0; i < task_count; i++) {
        if (!tasks[i].completed) {
            print_task(tasks[i]);
        } else {
            completed_count++;
        }
    }
    
    // 再打印已完成的任务
    if (completed_count > 0) {
        printf("\n已完成任务:\n");
        printf("----------------------------------------\n");
        for (int i = 0; i < task_count; i++) {
            if (tasks[i].completed) {
                print_task(tasks[i]);
            }
        }
    }
    
    printf("----------------------------------------\n");
    printf("总计: %d 个任务 (未完成: %d, 已完成: %d)\n", 
           task_count, 
           task_count - completed_count, 
           completed_count);
}

void mark_task_completed(int id) {
    for (int i = 0; i < task_count; i++) {
        if (tasks[i].id == id) {
            if (tasks[i].completed) {
                printf("任务 %d 已经是完成状态\n", id);
            } else {
                tasks[i].completed = 1;
                tasks[i].completed_at = time(NULL);
                printf("任务 %d 已标记为完成\n", id);
            }
            return;
        }
    }
    printf("错误: 找不到ID为 %d 的任务\n", id);
}

void delete_task(int id) {
    for (int i = 0; i < task_count; i++) {
        if (tasks[i].id == id) {
            // 删除任务，将后面的任务前移
            for (int j = i; j < task_count - 1; j++) {
                tasks[j] = tasks[j + 1];
            }
            task_count--;
            printf("任务 %d 已删除\n", id);
            return;
        }
    }
    printf("错误: 找不到ID为 %d 的任务\n", id);
}

void print_task(Task task) {
    char status[20];
    char date_str[64];
    
    if (task.completed) {
        strcpy(status, "✓ 已完成");
        strftime(date_str, sizeof(date_str), "%Y-%m-%d %H:%M:%S", localtime(&task.completed_at));
    } else {
        strcpy(status, "○ 未完成");
        strftime(date_str, sizeof(date_str), "%Y-%m-%d %H:%M:%S", localtime(&task.created_at));
    }
    
    printf("[%d] %s - %s (%s)\n", 
           task.id, 
           task.description, 
           status, 
           date_str);
}

void print_usage() {
    printf("\n使用方法:\n");
    printf("  todo add <任务描述>     - 添加新任务\n");
    printf("  todo list              - 列出所有任务\n");
    printf("  todo complete <ID>     - 标记任务为完成\n");
    printf("  todo delete <ID>       - 删除任务\n");
}